package com.yimils.core.goods.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yimils.core.goods.dto.EnvironmentAddReq;
import com.yimils.core.goods.dto.EnvironmentUpdateReq;
import com.yimils.core.goods.entity.EnvironmentEntity;
import com.yimils.core.goods.mapper.EnvironmentMapper;
import com.yimils.core.goods.serivce.EnvironmentService;
import com.yimils.core.goods.vo.EnvironmentRes;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service("environmentService")
public class EnvironmentServiceImpl extends ServiceImpl<EnvironmentMapper, EnvironmentEntity> implements EnvironmentService {
    @Override
    public Boolean saveEnvironment(EnvironmentAddReq environmentAddReq) {

        EnvironmentEntity environmentEntity = new EnvironmentEntity();
        environmentEntity.setEnvironmentName(environmentAddReq.getEnvironmentName());
        environmentEntity.setState(Boolean.FALSE);
        environmentEntity.setCreateTime(LocalDateTime.now());
        environmentEntity.setUpdateTime(LocalDateTime.now());
        return save(environmentEntity);
    }

    @Override
    public Boolean updateEnvironment(EnvironmentUpdateReq environmentUpdateReq) {
        QueryWrapper<EnvironmentEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(EnvironmentEntity::getId, environmentUpdateReq.getEnvironmentId()).eq(EnvironmentEntity::getState, Boolean.FALSE);
        EnvironmentEntity entity = getOne(qw);
        entity.setEnvironmentName(environmentUpdateReq.getEnvironmentName());
        entity.setUpdateTime(LocalDateTime.now());
        return saveOrUpdate(entity);
    }

    @Override
    public Boolean deleteEnvironment(List<Long> environmentIds) {
        QueryWrapper<EnvironmentEntity> qw = new QueryWrapper<>();
        qw.lambda().in(EnvironmentEntity::getId, environmentIds).eq(EnvironmentEntity::getState, Boolean.FALSE);
        List<EnvironmentEntity> environmentEntities = list(qw);
        if (CollectionUtils.isEmpty(environmentEntities)) {
            return Boolean.FALSE;
        }
        environmentEntities.forEach(o -> {
            o.setState(Boolean.FALSE);
            o.setUpdateTime(LocalDateTime.now());
        });
        return saveOrUpdateBatch(environmentEntities);
    }

    @Override
    public List<EnvironmentRes> listEnvironment() {

        QueryWrapper<EnvironmentEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(EnvironmentEntity::getState, Boolean.FALSE);
        List<EnvironmentEntity> environmentEntities = list(qw);
        return environmentEntities.stream().map(o -> {
            EnvironmentRes environmentRes = new EnvironmentRes();
            environmentRes.setEnvironmentName(o.getEnvironmentName());
            environmentRes.setEnvironmentId(o.getId());
            return environmentRes;
        }).collect(Collectors.toList());

    }

    @Override
    public List<EnvironmentEntity> listEnvironment(List<Long> environmentIds) {
        QueryWrapper<EnvironmentEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(EnvironmentEntity::getId, environmentIds).eq(EnvironmentEntity::getState, Boolean.FALSE);
        return list(qw);
    }
}
